<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box" v-show="!information">
			<view class="call-box u-m-t-2">
				<u-icon name="scan" @click="scan" size="48" class="u-m-r-30"></u-icon>
				<u-input v-model="keyword" placeholder="请输入用户手机号检测" class="u-m-r-30 u-flex-1" size="mini" clearable />
				<u-button type="primary" @click="searchKey(keyword)" size="mini">电话检索</u-button>
				<!-- <u-button @click="scan" size="mini" :plain="true" class="u-m-l-10">扫一扫</u-button> -->
			</view>
			<!-- 搜索结果 -->
			<view class="search-list">
				<view class="u-flex u-justify-content u-p-20" v-if="loading">
					<u-loading mode="flower"></u-loading><text class="u-tips-color u-m-l-10">搜索中...</text>
				</view>
				<view v-if="searchList.length > 0">
					<view class="u-m-l-30 u-m-t-10 u-m-b-10"><text class="u-tips-color u-font-24">共{{searchList.length}}条搜索结果</text></view>
					<u-cell-group>
						<u-cell-item 
							v-for="(item,index) in searchList"
							:key="item.qrcode"
							:title="item.name" :value="item.mobile"
							 @click="getUserDetails(item.id)">
						</u-cell-item>
					</u-cell-group>
				</view>
				<view class="u-p-60" v-if="!loading && searchList.length == 0">
					<view v-if="isNotReg">
						<u-empty text="没有检索出此用户" mode="search"></u-empty>
						<view class="u-text-center u-p-b-30">
							<text class="u-tips-color">是否以 {{keyword}} 号码创建新用户下单？</text>
						</view>
						<u-button type="primary" @click="registerAccount(keyword)">立即创建</u-button>
					</view>
					<u-empty v-else text="没有搜索结果" mode="search"></u-empty>
				</view>
			</view>
		</view>
		<!-- 下单用户信息 -->
		<view class="user-info-box" v-show="information">
			<view class="grid-box">
				<view class="head-title u-m-b-20"><text>下单用户信息</text></view>
				<u-form :model="form" ref="uForm" label-width="150rpx">
					<u-form-item style="color: #9fa3b0;"  label="下单用户" v-if="enterprise.id == 0">
						<u-input v-model="form.name" :disabled="true" type="select" @click="chooseEnterprise"/>
					</u-form-item>
					<u-form-item style="color: #9fa3b0;"  label="下单用户" v-else>
						<u-input v-model="enterprise.name" :disabled="true" type="select" @click="chooseEnterprise"/>
					</u-form-item>
					<u-form-item style="color: #9fa3b0;" label="电话号码">
						<u-input v-model="form.mobile" :disabled="true" type="select" @click="information = false"/>
					</u-form-item>
					
					<!-- <u-form-item style="color: #9fa3b0;" label="选择地址">
						<u-input v-model="form.address" :disabled="true" type="select" @click="showmap = false"/>
					</u-form-item> -->
					<u-form-item label="所在地区" prop="selectArea">
						<view class="u-flex" style="width: 100%;">
							<view class="u-flex-1" @click="cityshow = true" style="line-height: 1.4em;">
								<text v-if="form.selectArea">{{form.selectArea}}</text>
								<text v-else class="u-tips-color">请选择到街道级别</text>
							</view>
							<view @click="showmap = true" class="u-p-l-10 u-p-t-6" style="display: flex;align-items: center;">
								<u-icon name="map-fill" color="#10beca"></u-icon><text class="u-font-28 u-main-color u-p-l-5" style="color: #10beca;">定位</text>
							</view>
						</view>
					</u-form-item>
					<u-form-item style="color: #9fa3b0;" label="详情地址">
						<u-input v-model="form.address" />
					</u-form-item>
					<u-form-item style="color: #9fa3b0;" label="回收物拍照">
						<common-upload max-count="9" @on-success="uploadSuccess" @on-remove="removePhoto"></common-upload>
					</u-form-item>
				</u-form>
			</view>
			<!-- 用户地址（如果有） -->
			<view class="grid-box" v-if="useAddress.length > 0">
				<view class="head-title u-m-b-20"><text>选择地址</text></view>
				<view class="address-list u-p-t-20 u-p-b-20">
					<u-cell-group>
						<u-cell-item :arrow="false" class="item" v-for="(res, index) in useAddress" :key="res.id" @click="chooseAddress(res)">
							<view class="u-flex" :class="res.support?'':'none'">
								<view class="" style="margin-right: 30rpx;" v-if="res.id == form.addressId ">
									<u-icon color="rgb(16, 190, 202)" size="38"   name="checkmark-circle"></u-icon>
								</view>
								<view class="" style="margin-right: 30rpx;" v-if="res.id !== form.addressId ">
									<view class="yuan"></view>
								</view>
								<view class="u-flex-1">
									<view class="" style="display: flex; justify-content: space-between;">
										<view class="top">
											<view class="name">{{ res.name }}</view>
											<view class="">
												<view class="phone">{{ res.mobile }}</view>
											</view>
											<view class="tag" v-if="res.isdefault">
												<text class="bg-gradual-blue">默认</text>
											</view>
										</view>
										<view class="" @tap.stop="openEdit(res)" >
											<u-icon name="edit-pen" size="48"></u-icon>
										</view>
									</view>
									<view class="bottom u-line-1">
										{{res.address}}
									</view>
								</view>
							</view>
						</u-cell-item>
					</u-cell-group>
				</view>
			</view>
			
			<view class="grid-box"> 
				<view class="u-table-box">
					<view class="head-title u-m-b-20"><text>分拣清算</text></view>
					<u-button size="medium" plain @click="openPopup">+新增回收物</u-button>
					<goods-list :orderId="id" :listData="confirmForm.details" @upDate="upDate"></goods-list>
				</view>
				<view class="total" v-if="confirmForm.details.length > 0">
					<text>共{{confirmForm.details.length}}种回收品 合计:</text>
					<text class="total-price">
						￥{{!confirmForm.total?'--':confirmForm.total.toFixed(2)}}
					</text>
				</view>
			</view>
			<view class="u-p-l-30 u-p-r-30 u-m-t-30">
				<u-button type="primary" @click="goform" :loading="loading">提交</u-button>
			</view>
			<view class="u-p-30">
				<u-button @click="back">返回搜索</u-button>
			</view>
		</view>
		<u-popup v-model="chooseShow" mode="center" :closeable="true" border-radius="14" width="600rpx">
			<scroll-view scroll-y="true" style="max-height: 80vh;">
				<!-- <goods-box :orderId="id" :details="confirmForm.details" @confirm="confirmChoose"></goods-box> -->
				<goods-choose :orderId="id" :details="confirmForm.details" @confirm="confirmChoose"></goods-choose>
			</scroll-view>
		</u-popup>
		<citypicker ref="citypicker" :show="cityshow" :level="4" @select="getSelectCity" @close="close"></citypicker>
		<!-- 确认订单信息 -->
		<u-popup v-model="confirmOrder" mode="bottom" border-radius="14" :closeable="true" z-index="99">
			<view class="confirm-order u-p-t-40">
				<view class="u-text-center u-p-t-30">
					<view class="u-m-r-10"><u-icon name="question-circle-fill" size="120"
							class="u-type-warning"></u-icon></view>
					<view class="u-font-36">确认订单</view>
				</view>
				<view class="u-p-b-80 u-p-t-15 u-text-center u-font-28 u-tips-color"><text>请确认以下订单信息</text></view>
				<u-cell-group>
					<u-cell-item :arrow="false" v-for="(item,index) in confirmForm.details" :key="index">
						<view slot="title">
							<view class="u-font-30 u-main-color">
								<text class="goods-name">{{item.name}}</text>
							</view>
							<view class="u-font-24 u-tips-color">
								<text>单价：￥{{item.price}}元/{{ item.isweighing? 'KG':'件'}}</text>
								<text v-if="item.isweighing" class="u-m-l-30">重量：{{item.weight}}KG</text>
								<text v-else class="u-m-l-30">件数：{{item.weight}}件</text>
							</view>
						</view>
						<text class="u-font-28">
							<text class="goods-price">￥{{(item.price*item.weight).toFixed(2)}}</text>
						</text>
					</u-cell-item>
					<u-cell-item title="拍照上传" :arrow="false" v-if="form.pic">
						<view class="u-flex" style="justify-content: flex-end">
							<view class="u-p-l-10">
								<u-image :src="imgURL(form.pic)" width="100" height="100" @click="seePhoto(form.pic)">
									<u-loading slot="loading"></u-loading>
								</u-image>
							</view>
						</view>
					</u-cell-item>
				</u-cell-group>
				<view class="tips"><text>请确认无误后点击确认提交，提交后无法撤销。</text></view>
				<view class="navigation-sticky-box">
					<view class="navigation u-p-b-0 u-m-b-0">
						<view class="left">
							<view class="u-text-left u-flex-1 u-p-l-40">
								<view><text class="u-font-24 u-tips-color">确认后，将支付客户：</text></view>
								<text class="text-orange goods-price">￥</text>
								<text class="u-font-36 goods-price">{{confirmForm.total==0?'--':confirmForm.total.toFixed(2)}}</text>
							</view>
						</view>
						<view class="right" @tap="$u.throttle(startConfimCreate, 1000)">
							<view class="buy btn u-line-1 bg-gradual-blue">确认提交</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<!-- 此处用于获取扫码的用户类型为学校时，默认选中“学校统货”特定类别 -->
		<!-- <view style="display: none;">
			<goods-choose ref="typechoose" :orderId="id" :details="confirmForm.details" @confirm="confirmChoose"></goods-choose>
		</view> -->
		<!-- 确认支付方式 -->
		<u-popup v-model="payTypeShow" mode="center" border-radius="14" :closeable="true" width="600rpx" z-index="100">
			<view class="u-p-t-30 u-p-b-30 u-text-center"><text class="u-font-32">支付方式</text></view>
			<u-divider></u-divider>
			<u-cell-group>
				<u-cell-item title="微信小程序" :arrow="false" @click="payType = 1">
					<view slot="icon" class="u-flex" style="width: 80rpx;">
						<u-icon name="weixin-fill" size="60" color="#28a745"></u-icon>
					</view>
					<view slot="label">
						<view v-if="onlinePay"><text>支付至用户账户余额</text></view>
						<view v-else><text class="u-type-error">余额不足，请选择现金支付</text></view>
					</view>
					<view slot="right-icon">
						<view class="circle" :class="payType == 1?'on':''">
							<u-icon name="checkmark" size="20" color="#fff" v-if="payType == 1"></u-icon>
						</view>
					</view>
				</u-cell-item>
				<u-cell-item title="现金结算" :arrow="false" @click="payType = 2">
					<view slot="icon" class="u-flex" style="width: 80rpx;">
						<u-icon name="rmb-circle-fill" size="60" color="#fd7e14"></u-icon>
					</view>
					<view slot="label">
						<view><text>与用户线下现金结算</text></view>
					</view>
					<view slot="right-icon">
						<view class="circle" :class="payType == 2?'on':''">
							<u-icon name="checkmark" size="20" color="#fff" v-if="payType == 2"></u-icon>
						</view>
					</view>
				</u-cell-item>
			</u-cell-group>
			<view class="u-p-30">
				<u-button type="primary" @click="confirmCreate" :loading="loading">确认支付</u-button>
			</view>
		</u-popup>
		<!-- 选择客户身份（多个时） -->
		<u-popup v-model="userTypeShow" mode="center" border-radius="14" :closeable="true" width="600rpx" z-index="100">
			<view class="u-p-t-30 u-p-b-30 u-text-center"><text class="u-font-32">选择身份下单</text></view>
			<u-cell-group>
				<u-cell-item 
					v-for="(item,index) in userType.enterpriselist"
					:key="index"
					:title="item.name" 
					:label="item.type"
					:arrow="false"
					@click="enterprise = item"
					>
					<view slot="right-icon">
						<view class="circle" :class="enterprise.id == item.id?'on':''">
							<u-icon name="checkmark" size="20" color="#fff"></u-icon>
						</view>
					</view>
				</u-cell-item>
			</u-cell-group>
			<view class="u-p-30">
				<u-button type="primary" @click="userTypeShow = false">确定</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				//显示信息
				information:false,

				// 分拣清算
				chooseShow: false,
				confirmForm: {
					orderid: 0,
					details: [
						// {
						// 	id: 0,
						// 	name: '杂纸',
						// 	weight: 10,
						// 	price: 9.9
						// }
					],

					total: 0
				},
				id: 0,
				keyword: "",
				form: {
					name: '',
					mobile: '',
					citypath: '',
					citynames: '',
					selectarea: "",
					address: '',
					pic: '',
					pics: [],
					enterprisename:'',//客户组名称
					addressId: 0
				},
				showmap: false, //打开地图选择
				cityshow: false,
				// 搜索结果
				searchList: [],
				loading: false,
				isNotReg: false,
				confirmOrder: false,
				// 选择支付方式
				payTypeShow: false,
				payType: 1, //1:微信小程序,2:线下支付
				onlinePay: false,

				// 选择的客户信息
				info:{
					name: ''
				},
				// 客户的客户组信息
				enterprise:{
					id: 0,
					name: '', //客户组名称
					type: '', //客户组分类
					department: '', //客户组部门
				},
				// 选择用户身份（多个时）
				userTypeShow: false,
				userType:{
					enterpriselist: []
				},
				// 用户地址
				useAddress: []
			};
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail,
				header: state => state.Authorization,
				franchiseId: state=>state.franchiseId
			}),
			...mapState('brother', {
				goodsDetails: state => state.goodsDetails,
			})
		},
		watch:{
			goodsDetails(value){
				let confirmForm = {
					orderid: this.orderId,
					details: value
				}
				this.confirmChoose(confirmForm)
			},
			keyword(val){
				this.$u.throttle(this.searchKey(val), 500, false)
				// this.information = false
				this.form = {
					name: '',
					mobile: '',
					citypath: '',
					citynames: '',
					selectarea: "",
					address: '',
					pics: [],
					pic: '',
					enterprisename:'',//客户组名称
				}
				this.confirmForm.details = []
				this.confirmForm.total = 0
			},
			showmap(val){
				console.log('点击展示地图')
				if(val){
					this.showmap = false
					uni.chooseLocation({
						success: (res)=> {
							console.log('位置名称：' + res.name);
							console.log('详细地址：' + res.address);
							console.log('纬度：' + res.latitude);
							console.log('经度：' + res.longitude);
							
							this.form.address = res.name
							this.form.lat = res.latitude
							this.form.lng = res.longitude
							
							this.information = true
							// 逆地理编码
							this.loading = true
							const key = 'd1a7ea2dc24fa8ae4b0b10eac185ad5d'
							wx.request({
								url: `https://restapi.amap.com/v3/geocode/regeo?location=${res.longitude},${res.latitude}&key=${key}`,
								success: (res) => {
									console.log('regeo',res)
									
									let provinceName = res.data.regeocode.addressComponent.province
									let cityName = res.data.regeocode.addressComponent.city
									let districtName = res.data.regeocode.addressComponent.district
									let townshipName = res.data.regeocode.addressComponent.township
									
									let dataArr = [provinceName,cityName,districtName,townshipName]
									
									// 选中地址后自动选中所在地区
									return new Promise((resolve, reject) => {
										this.$refs.citypicker.getNextAreaData(0).then((rows)=>{
											rows.forEach((item)=>{
												if(item.name == provinceName){
													resolve()
													this.$refs.citypicker.chooseArea(item).then((city)=>{
														city.forEach((cityItem)=>{
															if(cityItem.name == cityName){
																resolve()
																this.$refs.citypicker.chooseArea(cityItem).then((county)=>{
																	county.forEach((countyItem)=>{
																		if(countyItem.name == districtName){
																			resolve()
																			this.$refs.citypicker.chooseArea(countyItem).then((town)=>{
																				this.form.cityid = town.find((item)=>item.name == townshipName)?.id
																				this.form.selectArea = provinceName + '-' + cityName + '-' + districtName + '-' + townshipName
																				this.$forceUpdate()
																			})
																		}else{
																			reject()
																		}
																	})
																})
															}else{
																reject()
															}
														})
													})
												}else{
													reject()
												}
											})
										})
									})
									.then(()=>{
										this.loading = false
									})
									.catch(()=>{
										this.loading = false
										this.form.address = ''
										uni.showModal({
											title:'提示',
											content: '该地区暂不支持服务'
										})
									})
								},
								fail: (err) => {
									console.log('err:',err)
								},
								complete: () => {
									uni.hideLoading()
									this.loading = false
								}
							})
							
							// uni.showToast({
							// 	title: '尽量填写详细地址，以便成功回收',
							// 	icon: 'none',
							// 	duration: 3000
							// })
						},
						fail: (err)=>{
							// uni.showModal({
							// 	title: '失败回调',
							// 	content: JSON.stringify(err)
							// })
							console.log('失败回调',err)
						},
						complete: (complete)=>{
							// uni.showModal({
							// 	title: '结束回调',
							// 	content: JSON.stringify(complete)
							// })
							
							console.log('结束回调',complete)
						}
					});
				}
			}
		},
		onBackPress(options) {
			if(this.information){
				this.back()
				return true
			}else{
				return false
			}
		},
		onLoad() {
			// this.getUsers()
			// this.GetUserByMobile('13750105622')
			
		},
		methods: {
			// 获取用户地址
			getUserAddress(id){
				return this.$u.get('/User/Address/List?id='+id).then((res)=>{
					this.useAddress = res.filter((item)=>item.support == true)
				})
			},
			// 选择用户地址
			chooseAddress(item) {
				console.log('选择用户地址',item)
				this.form.address = item.address
				this.form.lng = item.lng
				this.form.lat = item.lat
				this.form.cityid = item.cityid
				this.form.selectArea = item.citynames.replaceAll(',','-')
				
				this.form.addressId = item.id
			},
			// 注册客户
			registerAccount(mobile){
				this.$u.post('/User/Account/Register',{
					name: mobile,
					mobile: mobile,
					defaultPassType: 0
				}).then((res)=>{
					console.log('registerAccount',res)
					// uni.showModal({
					// 	content:'注册成功',
					// 	success: (res)=> {
					// 		if (res.confirm) {
					// 			this.searchKey(mobile).then(()=>{
					// 				let newUserId = this.searchList[0].id
					// 				this.getUserDetails(newUserId)
					// 			})
					// 		} else if (res.cancel) {
								
					// 		}
					// 	}
					// })
					uni.showToast({
						title: '注册成功'
					})
					this.searchKey(mobile).then(()=>{
						let newUserId = this.searchList[0].id
						this.getUserDetails(newUserId)
					})
				})
			},
			// // 获取常输入用户列表
			// getUsers(){
			// 	this.$u.get('/User/Account/GetUsers').then((res)=>{
			// 		this.searchList = res
			// 	})
			// },
			// 关键词搜索
			searchKey(key){
				this.searchList = []
				this.loading = true
				this.isNotReg = false
				return this.$u.post('/User/List',{
					key: key,
					isall: true
				}).then((res)=>{
					this.searchList = res.rows
					if(this.searchList.length == 0){
						if(this.$u.test.mobile(key)){
							this.isNotReg = true
						}
					}
					this.loading = false
				}).catch(()=>{
					this.loading = false
				})
			},
			back(){
				this.keyword = ''
				this.information = false
				this.searchList = []
				this.isNotReg = false
				this.confirmForm.details = []
				this.confirmForm.total = 0
				this.enterprise.id = 0
				this.enterpriselist = []
			},
			// 点击搜索的列表获取用户详情
			getUserDetails(id){
				this.$u.get('/User/Detail?userId='+id).then((res)=>{
					console.log('表示该用户下有加入的客户组再查看用户详情',res)
					this.form = {
						name: res.name,
						mobile: res.mobile,
						citypath: '',
						citynames: '',
						selectarea: "",
						address: '',
						pics: [],
						pic: '',
						enterprisename:'',//客户组名称
						...res
					}
					this.information = true
					this.info = res
					let enterpriselist = res?.bindinfo?.enterpriselist.filter((item)=>item.franchiseid == this.franchiseId) || []
					
					if(enterpriselist.length > 0){
						this.userType.enterpriselist = [{
							id: 0,
							name: this.info.name,
							type: '个人'
						}]
						enterpriselist.forEach((item)=>{
							this.userType.enterpriselist.push(item)
						})
						if(enterpriselist.length == 1){
							this.enterprise = {
								id:  enterpriselist[0].id, //应为客户组id
								name: enterpriselist[0].name,
								type: enterpriselist[0].type,
								department: enterpriselist[0].department,
							}
						}else{
							this.userTypeShow = true
						}
					}
					
					// 获取用户地址
					this.getUserAddress(id).then(()=>{
						if(this.useAddress.length > 0){
							this.chooseAddress(this.useAddress[0])
						}
					})
				})
			},
			// 选择企业
			chooseEnterprise(){
				if(this.enterprise.id > 0){
					this.userTypeShow = true
				}
			},
			// // 获取搜索用户的地址
			// getAddress(id){
			// 	this.$u.get('/User/Address/Details?id='+id).then((res)=>{
			// 		console.log('获取搜索用户的地址',res)
			// 	})
			// },
			scan() { //入库扫码
				// #ifdef H5
				this.getQRCodeData('8f17532c7bb848218230dfaf3df17cce')
				// #endif
				uni.scanCode({
					autoZoom: false,
					scanType: ['qrCode','barCode'],//可识别二维码与一维码
					success: (res) => {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						this.getQRCodeData(res.result)
					},
					// fail: (err) => {
					// 	uni.showModal({
					// 		content: '识别错误，请再试一次',
					// 		complete: (res) => {
			
					// 		}
					// 	})
					// }
				});
			},
			//二维码识别用户
			getQRCodeData(message,isnew = false) {
				if(message){
					this.enterprise.id = 0
					let franchiseId = this.franchiseId
					// uni.showModal({
					// 	title: '功能开发中...'
					// })
					this.$u.get('/AIO/Account/GetQRCodeInfo?code='+message).then((res)=>{
						const info = res.account?.user
						this.form = {
							name: info.name,
							mobile: info.mobile,
							citypath: '',
							citynames: '',
							selectarea: "",
							address: '',
							pics: [],
							pic: '',
							enterprisename:'',//客户组名称
							...info
						}
						this.information = true
						this.info = res.account?.user
						let enterpriselist = this.info?.bindinfo?.enterpriselist.filter((item)=>item.franchiseid == franchiseId) || []
						if(enterpriselist.length > 0){
							this.userType.enterpriselist = [{
								id: 0,
								name: this.info.name,
								type: '个人'
							}]
							enterpriselist.forEach((item)=>{
								this.userType.enterpriselist.push(item)
							})
							if(enterpriselist.length == 1){
								this.enterprise = {
									id:  enterpriselist[0].id, //应为客户组id
									name: enterpriselist[0].name,
									type: enterpriselist[0].type,
									department: enterpriselist[0].department,
								}
							}else{
								this.userTypeShow = true
							}
						}
						
						// 获取用户地址
						this.getUserAddress(this.info.id).then(()=>{
							if(this.useAddress.length > 0){
								this.chooseAddress(this.useAddress[0])
							}
						})
					})
				}
			},
			goform() {
				let check = true
				// 检查回收物清单的重量是否填写
				try {
					if(!this.form.selectArea){
						uni.showToast({
							title: '请选择地区',
							icon: 'none'
						})
						throw Error()
					}
					if(!this.form.address){
						uni.showToast({
							title: '请填写地址',
							icon: 'none'
						})
						throw Error()
					}
				    this.confirmForm.details.forEach(item => {
						if(item.isweighing){
							if(item.weight == null || item.weight == 0){
								uni.showToast({
									title: '请填写'+item.name+'的重量',
									icon: 'none'
								})
							    throw Error()
							}
						}else{
							if(item.weight == null || item.weight == 0){
								uni.showToast({
									title: '请填写'+item.name+'的数量',
									icon: 'none'
								})
							    throw Error()
							}
						}
				        
				    })
				} catch(error) {
					check = false; 
				}
				
				if(!check){
					return
				}
				
				this.confirmOrder = true
			},
			// 获支付配额余额
			getQuota(){
				// return this.$u.get(`/Store/Details?id=${this.storeId}`).then((res)=>{
				// 	/*
				// 		quota				配额（0为不限）
				// 		amount				已付金额（已用配额）
				// 		balance				剩余配额(空为不限，0为没有钱了，不能微信支付)
				// 	*/
				// 	console.log('查询配额',res.quota)
				// 	console.log('已付金额',res.amount)
				// 	console.log('剩余配额',res.balance)
				// 	this.storeDetails = res
				// 	return res
				// })
				
				return this.$u.get(`/Brother/GetQuota?staffId=${this.userdetail.id}`).then((res)=>{
					console.log('创建订单前获取信息',res)
					console.log('加盟商剩余配额(空为不限，0为没有钱了，不能微信支付)',res.franchiseBalance)
					return res
				})
			},
			// 发起提交
			startConfimCreate(){
				this.getQuota().then((res)=>{
					// 判断是否有微信支付配额
					if(res.balance == null || res.balance > 0){
						// 可使用微信支付
						this.onlinePay = true
						this.payType = 1
						this.payTypeShow = true
					}else{
						// 不可以使用微信支付
						this.onlinePay = false
						this.payType = 2
						this.payTypeShow = true
						// this.confirmCreate()
					}
				})
			},
			// 确认提交
			confirmCreate(){
				this.loading = true
				uni.showLoading({
					title: '提交中，请稍后...',
					mask: true
				})
				let params = {
					"userId": this.form.id,
					// "addressId": this.form.addressId,
					// "cityId": this.form.cityid,
					// "address": this.form.address,
					// "lng": this.form.lng,
					// "lat": this.form.lat,
					"pics": this.form.pics,
					"details": this.confirmForm.details,
					"remarks": '',
					"payWay": this.payType,
				}
				if(this.form.addressId > 0){
					params.addressId = this.form.addressId
				}else{
					params.cityId = this.form.cityid
					params.address = this.form.address
					params.lng = this.form.lng
					params.lat = this.form.lat
				}
				// 有客户组时默认为客户组下单
				if(this.info.id > 0 && this.enterprise.id == 0){
					params.userId = this.info.id
				}
				if(this.enterprise.id > 0){
					params.enterpriseId = this.enterprise.id
				}
				this.$u.post('/User/Order/Create', params).then((res) => {
					console.log(res)
					this.loading = false
					uni.hideLoading()
					uni.showModal({
						content: '下单成功',
						showCancel: false,
						success: function (res) {
							if (res.confirm) {
								uni.redirectTo({
									//关闭当前页面，跳转到应用内的某个页面。
									url:'/pages/brother/workbenches/workbenches'
								});
							}
						}
					})
				}).catch((err) => {
					uni.hideLoading()
					this.loading = false
				})
			},
			seePhoto(pic) {
				let urls = []
				urls.push(this.fileURL + pic)
				uni.previewImage({
					current: 0,
					urls: urls
				});
			},
			imgURL(img) {
				return this.fileURL + img
			},
			//图片
			uploadSuccess(data, index, lists, name) {
				// this.form.pic = data.datas.path
				this.form.pics.push(data.datas.path)
				console.log(this.form.pic)
				this.$forceUpdate()
			},
			removePhoto(index, lists, name) {
				// this.fileList.splice(index, 1)
				this.form.pics.splice(index, 1)
				// console.log(this.fileList)
			},
			// 上传图片失败时处理
			uploadError(data, index, lists, name) {
				uni.showToast({
					icon: 'none',
					title: '提交失败，请重试'
				})
			},
			// getPriceByOrder(smallTypeId) {
			// 	uni.showLoading({
			// 		title: '获取价格中...'
			// 	})
			// 	return this.$u.post(
			// 		`/waste/price/GetBrotherUserPrice?smallTypeId=${smallTypeId}&CityPath=${this.form.citypath}`).then(
			// 		(res) => {
			// 			uni.hideLoading()

			// 			return res
			// 		})
			// },
			// 关闭分拣清算窗口
			confirmChoose(confirmForm) {
				this.confirmForm.orderid = this.id
				let historyArr = []
				if(this.confirmForm.details.length > 0){
					historyArr = JSON.parse(JSON.stringify(this.confirmForm.details))
				}
				this.confirmForm.details = confirmForm.details

				this.confirmForm.total = 0
				confirmForm.details.forEach((item, index) => {
					// this.getPriceByOrder(item.id).then((datas) => {
					// 	console.log(datas)
					// 	console.log(this.confirmForm.details, '22222')
					// 	this.$set(this.confirmForm.details[index], 'allowedit', datas.allowedit)
					// 	this.$set(this.confirmForm.details[index], 'highprice', datas.highprice)
					// 	this.$set(this.confirmForm.details[index], 'minprice', datas.minprice)
						
					// 	if (datas.price == 0) {
					// 		this.$set(this.confirmForm.details[index], 'price', datas.minprice)
					// 	} else {
					// 		this.$set(this.confirmForm.details[index], 'price', datas.price)
					// 	}
					// 	historyArr.forEach((ha,i)=>{
					// 		if(ha.id == item.id){
					// 			item.weight = ha.weight
					// 			item.price = ha.price
					// 		}
					// 	})
					// })

					this.confirmForm.total += item.weight * item.price

				})
				this.chooseShow = false
			},
			upDate(confirmForm) {
				this.confirmForm.orderid = this.id
				this.confirmForm.details = confirmForm.details

				this.confirmForm.total = 0
				this.confirmForm.details.forEach((item, index) => {
					this.confirmForm.total += item.weight * item.price
				})
				this.chooseShow = false
			},
			openPopup() {
				// this.chooseShow = true
				this.$store.commit('brother/setGoodsDetails', this.confirmForm.details)
				uni.navigateTo({
					url: `/pages/brother/goodsChoose/goodsChoose?citypath=${this.form.citypath}`,
				});
				// if(this.orderForm.enterprise){
				// 	uni.navigateTo({
				// 		url: `/pages/brother/goodsChoose/goodsChoose?enterpriseid=${this.orderForm.enterprise.id}&citypath=${this.orderForm.address.citypath}`,
				// 	});
				// }else{
				// 	uni.navigateTo({
				// 		url: `/pages/brother/goodsChoose/goodsChoose?userid=${this.orderForm.user.id}&citypath=${this.orderForm.address.citypath}`,
				// 	});
				// }
			},
			close(state) {
				this.cityshow = state
			},
			getSelectCity(arr) { //城市选择回调函数
				console.log(arr)
				this.cityshow = false
				//this.selectArea = '';
				let cityidArr = [];
				let getArr = arr.filter(n => n.id != 0)
				let areaname = '';
				getArr.forEach((item, index) => {
					if (index == getArr.length - 1) {
						areaname += item.name
					} else {
						areaname += item.name + '-'
					}
					cityidArr.push(item.id);
				});
				if (cityidArr.length > 0)
					this.form.cityid = cityidArr[cityidArr.length - 1];
				if (areaname)
					this.form.selectArea = areaname;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.call-box {
		display: flex;
		align-items: center;
		background: #fff;
		padding: 30rpx;
	}
	.total{
		font-size: 30rpx;
		margin-top: 20rpx;
		
	}
	.total-price{
		font-size: 28rpx;
		    color: #fd7e14;
	}
	// 确认订单
	.confirm-order {
		max-height: 90vh;
	
		.price {
			font-weight: bold;
		}
	
		.bold {
			font-weight: bold;
		}
	
		.goods-name {
			font-weight: bold;
		}
	
		.goods-price {
			color: $u-color-orange;
			font-size: 32rpx;
			font-weight: bold;
		}
	
	
		.price-tag {
			display: flex;
			justify-content: flex-end;
	
			.item {
				display: block;
				padding: 0 10rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color: #3191fd;
			}
		}
	}
	// 提交订单栏
	.navigation-sticky-box {
		position: sticky;
		z-index: 99;
		bottom: 0;
		background: #fff;
		padding-bottom: 20rpx;
	
		.navigation {
			display: flex;
			width: 700rpx;
			// background-color: #ffffff;
			padding: 20rpx 0 30rpx;
			margin: 10rpx auto;
	
			.left {
				display: flex;
				align-items: center;
				font-size: 30rpx;
				flex: 1;
				background-color: #fff;
				border-top-left-radius: 50rpx;
				border-bottom-left-radius: 50rpx;
				box-shadow: $u-color-border-shadow;
	
				.item {
					margin: 0 30rpx;
					display: flex;
	
					.icon {
						position: relative;
					}
	
					&.car {
						text-align: center;
						position: relative;
	
						.car-num {
							position: absolute;
							top: -10rpx;
							right: -10rpx;
						}
					}
				}
			}
	
			.right {
				display: flex;
				font-size: 28rpx;
				align-items: center;
	
				.btn {
					line-height: 100rpx;
					padding: 0 30rpx;
					border-radius: 50rpx;
					color: #ffffff;
					border-top-left-radius: 0;
					border-bottom-left-radius: 0;
					width: 240rpx;
					text-align: center;
				}
	
				.cart {
					background-color: #ed3f14;
					margin-right: 30rpx;
				}
	
				.buy {
					background-color: #ff7900;
				}
			}
		}
	}

	// 提示
	.tips {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 16rpx 30rpx;
		border-radius: 8rpx;
		position: relative;
		transition: all 0.3s linear;
		border: 1px solid #fff;
		background-color: #fdf6ec;
		border-color: #fcbd71;
		font-size: 26rpx;
		text-align: left;
		color: #606266;
		margin: 30rpx 30rpx 0 30rpx;
	}
	.circle{
		width: 40rpx;
		height: 40rpx;
		border: 1px solid #666;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		&.on{
			border-color: $u-color-blue;
			background-color: $u-color-blue;
		}
	}
	// 用户地址
	.address-list{
		.yuan{
			width: 36rpx;
			height: 36rpx;
			background-color: white;
			border: 4rpx solid rgb(16, 190, 202);
			border-radius: 20rpx;
		}
		.item {
			// padding: 40rpx 20rpx;
			background-color: #ffffff;
			width: 100%;
		
			// border-bottom: 1px solid $u-border-color;
			.none {
				opacity: 0.4;
			}
		
			.top {
				display: flex;
				font-weight: bold;
				font-size: 32rpx;
				color: #000;
		
				.phone {
					margin-left: 50rpx;
				}
		
				.tag {
					display: flex;
					font-weight: normal;
					align-items: center;
		
					text {
						display: block;
						width: 60rpx;
						height: 34rpx;
						line-height: 34rpx;
						color: #ffffff;
						font-size: 20rpx;
						border-radius: 6rpx;
						text-align: center;
						margin-left: 30rpx;
						background-color: rgb(49, 145, 253);
					}
		
					.red {
						background-color: red
					}
				}
			}
		
			.bottom {
				// display: flex;
				// margin-top: 20rpx;
				font-size: 28rpx;
				// justify-content: space-between;
				color: #999999;
				width: 610rpx;
				text-align: left;
			}
		}
	}
</style>
